const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
  mode: 'development',
  entry: path.join(__dirname, 'src/main.js'),
  // 定义出口
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.[hash:8].js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          {
            loader: 'style-loader',
            options: {
              insert: 'body'
            }
          },
          'css-loader'
        ]
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          'css-loader',
          'less-loader'
        ]
      },
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          /* options: {
            presets: [
              ['@babel/preset-env', { targets: "defaults" }]
            ]
          } */
        }
      },
      {
        test: /\.(png|jpe?g|gif)$/i,
        type: 'asset',
        parser: {
          dataUrlCondition: {
            maxSize: 50 * 1024 // 200kb
          }
        }
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: '今天学习webpack真好玩',
      template: path.join(__dirname, 'public/index.html')
    }),
    new CleanWebpackPlugin()
  ],
  resolve: {
    alias: {
      "@": path.join(__dirname, 'src')
    },
    extensions: ['.js', '.json', '.vue']
  },
  devServer: {
    static: {
      directory: path.join(__dirname, 'public'),
    },
    // 是否开启 gzip压缩
    compress: true,
    port: 9000,
    open: true
  }
}